package cn.keniykey.mc.landterritory.landflag.api;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Nullable;

import org.bukkit.configuration.file.YamlConfiguration;

import cn.keniykey.mc.landterritory.LandTerritoryMain;
import cn.keniykey.mc.landterritory.landflag.LandFlag;
import cn.keniykey.mc.landterritory.toolkit.DebugUtil;

public final class LandFlagConfigurationFile {
	private static final String FIELD_NAME = "flags_config.yml";
	private static YamlConfiguration yaml;
	private final String flagname;
	
	private static boolean haschange = false;
	
	LandFlagConfigurationFile(LandFlag flag) {
		this.flagname = flag.getDescription().getName();
	}
	
	public static void load() {
		final File file = new File(LandTerritoryMain.getPlugin().getDataFolder(), FIELD_NAME);
		yaml = file.exists() ? YamlConfiguration.loadConfiguration(file) : new YamlConfiguration();
		save();
	}
	public static void changeSave() {
		if(haschange) save();
		haschange = false;
	}
	/**
	 * 若是onPublicConfigLoad方法中，可以不用调用
	 * 等所有标记执行完后，会自动调用一次
	 */
	public static void save() {
		final File file = new File(LandTerritoryMain.getPlugin().getDataFolder(), FIELD_NAME);
		try {
			yaml.save(file);
		} catch (IOException ex) {
			ex.printStackTrace();
			DebugUtil.info("公共配置文件[ConfigurationFile]无法保存");
		}
	}
	public boolean has() {
		return yaml.contains(this.flagname);
	}
	public void set(String key,Object obj) {
		if(key == null || key.isEmpty()) {
			yaml.set(this.flagname, obj);
		}else {
			yaml.set(this.flagname + "." + key, obj);
		}
		haschange = true;
	}
	private String getKeyName(String key) {
		return (key == null || key.isEmpty() ? this.flagname : this.flagname + "." + key);
	}
	@Nullable
	public String getString(String key) {
		return yaml.getString(this.getKeyName(key));
	}
	public List<String> getStringList(String key) {
		return yaml.getStringList(this.getKeyName(key));
	}
	public int getInt(String key) {
		return yaml.getInt(this.getKeyName(key));
	}
	public List<Integer> getIntList(String key) {
		return yaml.getIntegerList(this.getKeyName(key));
	}
	public long getLong(String key) {
		return yaml.getLong(this.getKeyName(key));
	}
	public List<Long> getLongList(String key) {
		return yaml.getLongList(this.getKeyName(key));
	}
	public double getDouble(String key) {
		return yaml.getDouble(this.getKeyName(key));
	}
	public List<Double> getDoubleList(String key) {
		return yaml.getDoubleList(this.getKeyName(key));
	}
	public boolean getBoolean(String key) {
		return yaml.getBoolean(this.getKeyName(key));
	}
	public List<Boolean> getBooleanList(String key) {
		return yaml.getBooleanList(this.getKeyName(key));
	}
	public boolean contains(String key) {
		return yaml.contains(this.getKeyName(key));
	}
	public Set<String> getKeys(boolean deep) {
		final Set<String> newkeys = new HashSet<>();
		for(String key : yaml.getKeys(true)) {
			if (!key.startsWith(this.flagname)) continue;
			if (deep) {
				newkeys.add(key.replace(this.flagname + ".", "")); //获取全部
			}else {
				newkeys.add(key.split("\\.")[1]); //仅获取头文件
			}
		}
		return newkeys;
	}
}